package map.utils;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.zjw.zy.R;
import com.zjw.zy.utils.AppUtils;

import map.MapActivity;
import map.coreui.fragment.MapBaseFragment;
import map.coreui.fragment.MapFragment;

public final class MapPageManager {

    private static Fragment sRoot;
    private static FragmentManager sFragmentManager;


    public static void init(@NonNull MapActivity mapActivity) {
        sRoot = mapActivity.getSupportFragmentManager().findFragmentById(R.id.nav_host_map);
        if (sRoot == null) {
            throw new RuntimeException("sRoot == null");
        }

        sFragmentManager = sRoot.getChildFragmentManager();
    }

    public static MapBaseFragment getTopFragment() {
        if (sRoot == null) {
            throw new RuntimeException("sRoot == null");
        }
        return (MapBaseFragment) sFragmentManager.getPrimaryNavigationFragment();
    }

    public static void goMainMap(@Nullable Bundle args) {
        if (getTopFragment() instanceof MapFragment) {
            return;
        }
        AppUtils.getMapActivity().navigationController().popBackStack();
        AppUtils.getMapActivity().navigationController().navigate(R.id.nav_map_main_fragment);
    }
}
